import { GetScoreLottery, SetScoreLottery } from "~/api";

export const useContent = () => {
    const dialogStore = useDialogStore();
    const userStore = useUserStore();
    const lotteryActive = ref(0);
    const lotteryMap = ref([]);
    const isLoop = ref(false);

    const onClose = () => {
        dialogStore.isScoreLotteryDialogVisible = false;
    };

    const startLottery = (ms, i, callback) => {
        setTimeout(() => {
            if (lotteryActive.value < lotteryMap.value.length - 1) {
                lotteryActive.value += 1;
            } else {
                lotteryActive.value = 0;
            }
            if (ms < lotteryMap.value.length * 10 * 3 + i * 10)
                startLottery(ms + 10, i, callback);
            else {
                // lotteryActive.value = 0;
                typeof callback == "function" && callback();
            }
        }, ms);
    };

    const onStartLottery = async () => {
        isLoop.value = true;
        const res = await SetScoreLottery();
        if (res.code == 200) {
            let i = lotteryMap.value.findIndex((item) => item.code == res.data);
            lotteryActive.value = 0;
            startLottery(100, i + 1, function () {
                const lotteryItem = lotteryMap.value[i];
                if (lotteryItem.type == "thanks") {
                    dialogStore.isScoreLotteryFailDialogVisible = true;
                } else {
                    dialogStore.isScoreLotterySuccessDialogVisible = true;
                    dialogStore.scoreSuccessDialogState = lotteryItem;
                }
                GET_SCORE_LOG();
                isLoop.value = false;
            });
            GET_MIDNUM();
        } else {
            Message().warning(res.msg);
        }
    };

    const tipsDialog = (callback) => {
        Dialog({
            title: "提示",
            message: "抽奖每次需要100积分",
            closeOnClickOverlay: false,
        }).then((type) => {
            if (type === "confirm") {
                typeof callback == "function" && callback();
            }
        });
    };

    const onStart = async () => {
        if (isLoop.value) return false;
        const lotteryCache = localStorage.getItem("lottery");
        if (lotteryCache) {
            const lottery = JSON.parse(lotteryCache);
            if (
                new Date().toDateString() === new Date(lottery).toDateString()
            ) {
                onStartLottery();
                localStorage.setItem("lottery", Date.now());
            } else {
                tipsDialog(function () {
                    onStartLottery();
                    localStorage.setItem("lottery", Date.now());
                });
            }
        } else {
            tipsDialog(function () {
                onStartLottery();
                localStorage.setItem("lottery", Date.now());
            });
        }
    };

    watch(
        () => dialogStore.isScoreLotteryDialogVisible,
        (newVal) => {
            if (newVal) {
            }
        }
    );

    const GetLottery = async () => {
        const res = await GetScoreLottery();
        lotteryMap.value = res.data;
    };

    return {
        lotteryActive,
        lotteryMap,
        GetLottery,
        onClose,
        onStart,
    };
};
